package com.itqf.controller;

import com.google.gson.Gson;
import com.itqf.entity.*;
import com.itqf.service.AddressService;
import com.itqf.service.CartService;
import com.itqf.service.OrdersService;
import com.itqf.service.impl.AddressServiceImpl;
import com.itqf.service.impl.CartServiceImpl;
import com.itqf.service.impl.ItemServiceImpl;
import com.itqf.service.impl.OrdersServiceImpl;
import com.itqf.utils.Constants;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.List;

@WebServlet("/order")
public class OrdersController extends BaseServlet{

    public String preView(HttpServletRequest request, HttpServletResponse response) throws SQLException, InvocationTargetException, IllegalAccessException {

//        1. 获取请求参数uid
        String uid = request.getParameter("uid");
//        2. 调用业务逻辑 （地址，购物车）
        AddressService addressService = new AddressServiceImpl();
        List<Address> addressList = addressService.findAddressByUid(Integer.parseInt(uid));

        CartService cartService = new CartServiceImpl();
        List<Cart> cartList = cartService.findAll(Integer.parseInt(uid));
//        3. 放入共享域即可
        request.setAttribute("addressList",addressList);
        request.setAttribute("cartList",cartList);

        return Constants.FORWARD + "/order.jsp";
    }

    public String create(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

//        1. 获取请求参数
        String aid = request.getParameter("aid");
        String uid = request.getParameter("uid");
        String sum = request.getParameter("sum");

//        2. 调用业务逻辑     生成订单
        OrdersService ordersService = new OrdersServiceImpl();
        ordersService.createOrder(aid,uid,sum);

//        3. 转发到订单展示的方法

        return Constants.FORWARD+"order?method=show";
    }

    public String show(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute("loginUser");
        if(user == null){
            session.setAttribute("msg","登录后可以查看订单！");
            return Constants.REDIRECT+"/login.jsp";
        }
        OrdersService ordersService = new OrdersServiceImpl();

        List<Orders> ordersList = ordersService.findOrdersByUid(user.getUid());
        request.setAttribute("ordersList",ordersList);
        return Constants.FORWARD+"/orderList.jsp";
    }

    public String detail(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {

        String oid = request.getParameter("oid");

        OrdersService ordersService = new OrdersServiceImpl();
        Orders order = ordersService.findOrderByOid(oid);
        request.setAttribute("order",order);
//        System.out.println(order);
        return Constants.FORWARD+"/orderDetail.jsp";
    }
    public String successConfirm(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {
        String oid = request.getParameter("oid");
        OrdersService ordersService = new OrdersServiceImpl();
        Orders order = ordersService.successConfirmOrderByOid(oid);
        request.setAttribute("order",order);
        return Constants.FORWARD+"/orderDetail.jsp";
    }

    public String urgeGoods(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {
        String oid = request.getParameter("oid");
        OrdersService ordersService = new OrdersServiceImpl();
        Orders order = ordersService.urgeGoodsByOid(oid);
        request.setAttribute("order",order);
        return Constants.FORWARD+"/orderDetail.jsp";
    }

    public String success(HttpServletRequest request,HttpServletResponse response) throws SQLException {
//        1. 获取请求参数
        String oid = request.getParameter("oid");
        String odetail = request.getParameter("odetail");
        String result = request.getParameter("result");
//        System.out.println("oid="+oid+"result="+result);
//        System.out.println("SUCCESS".equals(result));

        if(odetail==null){
            odetail="用户没有添加任何备注信息";
        }
        if(result!=null && result.equals("SUCCESS")){
//            System.out.println("http://localhost:8080/order?method=success&oid=20221104183237718&result='SUCCESS'");
//            System.out.println(odetail);
//            支付成功
//            修改状态
//            返回订单列表页面
            OrdersService ordersService = new OrdersServiceImpl();
            ordersService.updateStateByOid(odetail,oid);
            return Constants.FORWARD+"/order?method=show";
        }else {
//            支付失败
            HttpSession session = request.getSession();
            session.setAttribute("msg","订单："+oid+" 支付失败！");
//            到 message.jsp 页面
            return Constants.REDIRECT + "/message.jsp";

        }
    }

    public String addOrder(HttpServletRequest request,HttpServletResponse response){

        return Constants.REDIRECT + "addProduct.jsp";
    }

    public String deleteOrder(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        String oid = request.getParameter("oid");
//        删除订单项
        ItemServiceImpl itemService = new ItemServiceImpl();
        itemService.deleteItemByOid(oid);
        System.out.println(oid);
//        删除订单
        OrdersService ordersService = new OrdersServiceImpl();
        ordersService.deleteOrderByOid(oid);
        return Constants.FORWARD+"/order?method=show";
    }

    public String returnOrder(HttpServletRequest request,HttpServletResponse response) throws IllegalAccessException, SQLException, InvocationTargetException {
        String oid = request.getParameter("oid");
        OrdersService ordersService = new OrdersServiceImpl();
        Orders order = ordersService.returnOrderByOid(oid);
        request.setAttribute("order",order);
        return Constants.FORWARD+"/orderDetail.jsp";
    }

}
